﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PropertyManagement.Core;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Modal {
    public partial class AddHouseOwner : Form {
        HouseOwner CurrentHouseOwner { get; set; }
        HouseOwnerManage HouseOwnerManage { get; set; }
        public AddHouseOwner( ) {
            InitializeComponent( );
           HouseOwnerManage = new HouseOwnerManage();
        }

        public AddHouseOwner(HouseOwner owner):this() {
            CurrentHouseOwner = owner;
            
        }

        Action Callback { get; set; }

        public AddHouseOwner(HouseOwner owner, Action action) : this(owner) {
            if (action != null) {
                Callback = action;
            }
        }

        private void ResetEditModel( ) {
            if (CurrentHouseOwner != null) {
                textBox1.Enabled = false;
                textBox1.Text = CurrentHouseOwner.HouseCode;
                textBox2.Text = CurrentHouseOwner.ApartmentName;
                textBox3.Text = CurrentHouseOwner.Floor.ToString();
                textBox4.Text = CurrentHouseOwner.OwnerName;
                textBox5.Text = CurrentHouseOwner.OwnerPhoneNumber;
                textBox6.Text = CurrentHouseOwner.OwnerIdentityCard;
                textBox7.Text = CurrentHouseOwner.Remark;
                comboBox1.SelectedIndex = (int) CurrentHouseOwner.HouseType - 1;
                comboBox2.SelectedItem = CurrentHouseOwner.SpaceArea.ToString();
            }
            else {
                comboBox1.SelectedIndex = 0;
                comboBox2.SelectedIndex = 0;
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            
            try {
                if (CurrentHouseOwner != null) {
                    CurrentHouseOwner.ApartmentName = textBox2.Text.Trim();
                    CurrentHouseOwner.Floor = int.Parse(textBox3.Text.Trim());
                    CurrentHouseOwner.OwnerName = textBox4.Text.Trim();
                    CurrentHouseOwner.OwnerPhoneNumber = textBox5.Text.Trim();
                    CurrentHouseOwner.OwnerIdentityCard = textBox6.Text.Trim();
                    CurrentHouseOwner.Remark = textBox7.Text.Trim();
                    CurrentHouseOwner.HouseType = (HouseType) (comboBox1.SelectedIndex + 1);
                    CurrentHouseOwner.SpaceArea = int.Parse(comboBox2.SelectedItem.ToString( ));
                    CurrentHouseOwner.LastUpdate = DateTime.Now;
                    CurrentHouseOwner.LastActionUserID = GlobalSetting.CurrentSystemUser.SystemUserID;
                    CurrentHouseOwner.EffectiveDate = dateTimePicker1.Value.Date;
                    
                        HouseOwnerManage.Update(CurrentHouseOwner);
                        FormHelper.ShowInfoMsg("编辑住户成功");
                        if (Callback != null) {
                            Callback();
                        }
                        this.Close();
                   

                }
                else {
                    CurrentHouseOwner = new HouseOwner();
                    CurrentHouseOwner.HouseCode = textBox1.Text.Trim();
                    CurrentHouseOwner.ApartmentName = textBox2.Text;
                    CurrentHouseOwner.Floor = int.Parse(textBox3.Text.Trim());
                    CurrentHouseOwner.OwnerName = textBox4.Text.Trim();
                    CurrentHouseOwner.OwnerPhoneNumber = textBox5.Text.Trim();
                    CurrentHouseOwner.OwnerIdentityCard = textBox6.Text.Trim();
                    CurrentHouseOwner.Remark = textBox7.Text.Trim();
                    CurrentHouseOwner.HouseType = (HouseType) (comboBox1.SelectedIndex + 1);
                    CurrentHouseOwner.SpaceArea = int.Parse(comboBox2.SelectedItem.ToString());
                    CurrentHouseOwner.CreationDate = CurrentHouseOwner.LastUpdate = DateTime.Now;
                    CurrentHouseOwner.CreationUserID =
                        CurrentHouseOwner.LastActionUserID = GlobalSetting.CurrentSystemUser.SystemUserID;
                    CurrentHouseOwner.EffectiveDate = dateTimePicker1.Value.Date;
                   
                        if (HouseOwnerManage.Add(CurrentHouseOwner)) {
                            FormHelper.ShowInfoMsg("添加住户成功");
                            if (Callback != null) {
                                Callback();
                            }
                            this.Close();
                        }
                        else {
                            FormHelper.ShowErrorMsg("添加住户失败");
                        }
                    
                }
            }
            catch {
                CurrentHouseOwner = null;
                FormHelper.ShowErrorMsg("操作住户失败，请检查输入或联系系统管理员");
            }

        }

        private void button2_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void textBox1_Leave(object sender, EventArgs e) {
            if (textBox1.Text.Trim().Length != 6) {
                label10.Text = "门牌号码必须6个字符";
                button1.Enabled = false;
            }
            else {
                 button1.Enabled = true;
                label10.Text = "";
            }
           
        }

        private void textBox3_Leave(object sender, EventArgs e) {
            int i = 0;
            if (!int.TryParse(textBox3.Text.Trim(), out i)) {
                label12.Text = "楼层必须输入数字";
                button1.Enabled = false;
            } else {
                button1.Enabled = true;
                label12.Text = "";
            }
        }

        private void textBox4_Leave(object sender, EventArgs e) {
            if (textBox4.Text.Trim().Length > 8 || textBox4.Text.Trim().Length == 0) {
                label15.Text = "姓名不能为空或多于4个中文字符";
                button1.Enabled = false;
            }
            label15.Text = "";
        }

        private void textBox5_Leave(object sender, EventArgs e) {
            if (!Helper.ValidatePhoneNumber(textBox5.Text.Trim())) {
                label16.Text = "请输入正确的电话号码";
                button1.Enabled = false;
            } else {
                button1.Enabled = true;
                label16.Text = "";
            }
        }

        private void textBox6_Leave(object sender, EventArgs e) {
            if (!Helper.ValidateIdentityCard(textBox6.Text.Trim( ))) {
                button1.Enabled = false;
                label17.Text = "请输入正确的身份证号码";
            } else {
                button1.Enabled = true;
                label17.Text = "";
            }
        }

        private void AddHouseOwner_Load(object sender, EventArgs e) {
            ResetEditModel( );
            if (CurrentHouseOwner != null) {
                this.Text = "编辑住户";
            }
        }

    



        


    }
}
